package query;

import gui.CalendarWindow;
import gui.Main;

import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;
import java.util.ArrayList;

import no.ntnu.fp.model.CurrentUser;
import no.ntnu.fp.model.Message;
import no.ntnu.fp.model.MessageType;
import no.ntnu.fp.model.Person;

/**
 * Class for accsessing the Melding(Message) table in the database
 * 
 * @author Tom
 *
 */
public class MessageQuery {
	private static ResultSet rs;
	/**
	 * making a new message in the database... what did you think it did? pet kittens? GOD you are so dumb, so dumb...
	 * 
	 * @param Message
	 *  - The Message you want to insert
	 */
	public static void insertMessage(Message Message){
		try {
			Statement stmt = Main.con.createStatement();
			String sql = "INSERT INTO MELDING(til, fra, Subject, tekst, type, moteid) VALUES( " + Message.getTo().getId() + "," + Message.getFrom().getId() +
			 ", '" +  Message.getSubject() + "','" + Message.getText() + "', '" + Message.getType().toString() + "'";
			if ( Message.getMoteID() == 0){
			sql = sql+ ", null)";
			}
			else {
				sql = sql + "," + Message.getMoteID() + ")";
			}
			System.out.println(sql);
			stmt.executeUpdate(sql);
		
		} catch (SQLException e) {
			e.printStackTrace();
		}
	} 
	/**
	 * Method for asking the Datebase for messages that is too you.
	 * 
	 * @param me Person
	 *  - The Persons messages you want.
	 * @return ArrayList<Message>
	 *  - Containing all(if any) messages bound to you.
	 */
	public static ArrayList<Message> getMyMessages(Person me){
		ArrayList <Message> mes = new ArrayList<Message>();
		Message melding;
		try {
			Statement stmt = Main.con.createStatement();
			rs = stmt.executeQuery("SELECT * FROM MELDING WHERE til = " + me.getId());	
			while(rs.next()){
				MessageType ty = MessageType.DELETEMEETING;
				System.out.println(rs.getString("type"));
				if(rs.getString("type").equals("Inkallelse")){
					ty = MessageType.SUMMON;
				}
				
			melding = new Message(me, PersonQuery.getPerson(rs.getInt("fra")), rs.getString("Subject"), rs.getString("tekst"), rs.getInt("MoteID"), rs.getInt("Meldingsid"), ty);
			mes.add(melding);
			}
		} catch (SQLException e) {
			e.printStackTrace();
		}
		return mes;
	}
	/**
	 * Metode for answereing a Summons
	 * 
	 * @param msg
	 *  - The Summons you wish to answere to.
	 * @param bool
	 *  - 1 for accsept, 0 for Not accsept, and <2and=<-1 for Creating a shitstorm.
	 */
	public static void AnswereSummons(Message msg, int bool){
		try {
			Statement stmt = Main.con.createStatement();
			stmt.executeUpdate("Update MoteDeltager SET Godtatt= " + bool + " WHERE DeltagerID= " + msg.getTo().getId() + " AND MoteID= "  + msg.getMoteID());
			
			CalendarWindow.updateLists();
			
			
		} catch (SQLException e) {
			
			e.printStackTrace();
		}
		
		
	}
	public static void AnswereSummons(int moteID, int personID, int bool){
		try {
			Statement stmt = Main.con.createStatement();
			String sql ="Update MoteDeltager SET Godtatt= " + bool + " WHERE DeltagerID= " + personID + " AND MoteID= "  + moteID;
			System.out.println(sql);
			stmt.executeUpdate(sql);
			
			
		} catch (SQLException e) {
			
			e.printStackTrace();
		}
		
		
	}
	public static int isAnswered(int personID, int moteID){
		Statement stmt;
		try {
			stmt = Main.con.createStatement();
			rs = stmt.executeQuery("SELECT Godtatt FROM moteDeltager WHERE DeltagerID = " + personID + " AND MoteID =" +  moteID);	
			if(rs.next()){
			return rs.getInt("Godtatt");
			}
			else {
				return -2;
			}
			
		} catch (SQLException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
			return -1;
		}
	}
}
